package cc.wfu.domain.order.service;

import cc.common.types.entity.ProductActivityEntity;
import cc.wfu.domain.order.model.aggregate.CreateOrderAggregate;
import cc.wfu.domain.order.model.entity.PayOrderEntity;
import cc.wfu.domain.order.model.entity.ProductActivitySkuEntity;
import cc.wfu.domain.order.model.entity.SkuRechargeOrderEntity;
import cc.wfu.domain.order.repository.IProductActivityRepository;
import cc.wfu.domain.order.service.rule.IActionChain;
import cc.wfu.domain.order.service.rule.factory.DefaultActivityChainFactory;
import cc.wfu.types.common.Constants;
import cc.wfu.types.enums.ResponseCode;
import cc.wfu.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Author: jia-qiang ljq1024.cc
 * @desc: 商品抽象类，定义标准的下单流程
 * @Date: 2024-06-05-21:52
 */
@Slf4j
public abstract class AbstractProductActivity extends ProductActivitySupport implements IProductOrder {

    /**
     * @param skuRechargeEntity 商品实体对象
     * @return 支付二维码 url
     */
    @Override
    public String createSkuRechargeOrder(SkuRechargeOrderEntity skuRechargeEntity) {
        // 1. 参数校验
        Long sku = skuRechargeEntity.getSku();
        String userId = skuRechargeEntity.getUserId();
        String outBusinessNo = skuRechargeEntity.getOutBusinessNo();
        if (sku == null || !StringUtils.hasLength(userId) || !StringUtils.hasLength(outBusinessNo)) {
            throw new AppException(ResponseCode.ILLEGAL_PARAMETER.getCode(), ResponseCode.ILLEGAL_PARAMETER.getInfo());
        }
        // 2. 查询基础信息
        // 2.1 通过sku查询活动商品sku信息
        ProductActivitySkuEntity activitySkuEntity = queryActivitySku(sku);
        // 2.2 查询活动商品信息
        ProductActivityEntity activityEntity = queryProductActivityByActivityId(activitySkuEntity.getProductId());
        // 3. 商品活动动作规则校验
        IActionChain actionChain = defaultActivityChainFactory.openActionChain();
        boolean success = actionChain.action(activitySkuEntity, activityEntity);
        if (success) {
            // 4. 生成订单聚合对象
            CreateOrderAggregate createOrderAggregate = buildOrderAggregate(skuRechargeEntity, activitySkuEntity, activityEntity /*, activityCountEntity*/);
            // 5. 生成支付订单
            PayOrderEntity payOrderEntity = createOrder(createOrderAggregate);
            // 6. 返回支付url
            return payOrderEntity.getPayUrl();
        }
        throw new AppException(Constants.ResponseCode.ACTIVITY_CHECK_ERROR.getInfo());
    }

    /**
     * 校验通过，开始生成订单 - 二维码扫码支付
     */
    protected abstract PayOrderEntity createOrder(CreateOrderAggregate createOrderAggregate);

    /**
     * 变更；订单支付关闭
     * @param orderId 订单ID
     */
    @Override
    public boolean changeOrderClose(String orderId) {
        return activityRepository.changeOrderClose(orderId);
    }

    /**
     * 查询超时订单列表
     * @return 超时订单单号列表
     */
    @Override
    public List<String> queryTimeoutCloseOrderList() {
        return activityRepository.queryTimeoutCloseOrderList();
    }

    /**
     * 查询有效期内，未接收到支付回调的订单
     */
    @Override
    public List<String> queryNoPayNotifyOrder() {
        return activityRepository.queryNoPayNotifyOrder();
    }

    /**
     * 查询待补货订单
     */
    @Override
    public List<String> queryReplenishmentOrder() {
        return activityRepository.queryReplenishmentOrder();
    }

    /**
     * 订单商品发货
     * @param orderId 订单ID
     */
    @Override
    public void deliverGoods(String orderId) {
        activityRepository.deliverGoods(orderId);
    }


    protected abstract CreateOrderAggregate buildOrderAggregate(SkuRechargeOrderEntity skuRechargeEntity, ProductActivitySkuEntity activitySkuEntity, ProductActivityEntity activityEntity);
}
